package com.example.controller;

import com.example.dto.RiskIndicatorDTO;
import com.example.dto.RiskIndicatorQuery;
import com.example.service.RiskIndicatorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 风控指标REST控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/risk-indicators")
@RequiredArgsConstructor
public class RiskIndicatorController {

    private final RiskIndicatorService riskIndicatorService;

    /**
     * 创建风控指标
     */
    @PostMapping
    public ResponseEntity<RiskIndicatorDTO> createIndicator(@RequestBody RiskIndicatorDTO indicatorDTO) {
        log.info("Creating risk indicator: {}", indicatorDTO);
        RiskIndicatorDTO savedIndicator = riskIndicatorService.saveIndicator(indicatorDTO);
        return new ResponseEntity<>(savedIndicator, HttpStatus.CREATED);
    }

    /**
     * 批量创建风控指标
     */
    @PostMapping("/batch")
    public ResponseEntity<List<RiskIndicatorDTO>> createIndicators(@RequestBody List<RiskIndicatorDTO> indicatorDTOs) {
        log.info("Creating {} risk indicators", indicatorDTOs.size());
        List<RiskIndicatorDTO> savedIndicators = riskIndicatorService.saveIndicators(indicatorDTOs);
        return new ResponseEntity<>(savedIndicators, HttpStatus.CREATED);
    }

    /**
     * 根据ID获取风控指标
     */
    @GetMapping("/{id}")
    public ResponseEntity<RiskIndicatorDTO> getIndicator(@PathVariable String id) {
        log.info("Getting risk indicator with ID: {}", id);
        return riskIndicatorService.findById(id)
                .map(indicator -> new ResponseEntity<>(indicator, HttpStatus.OK))
                .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 根据用户ID获取风控指标
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<RiskIndicatorDTO>> getUserIndicators(@PathVariable String userId) {
        log.info("Getting risk indicators for user: {}", userId);
        List<RiskIndicatorDTO> indicators = riskIndicatorService.findByUserId(userId);
        return new ResponseEntity<>(indicators, HttpStatus.OK);
    }

    /**
     * 根据查询条件搜索风控指标
     */
    @PostMapping("/search")
    public ResponseEntity<List<RiskIndicatorDTO>> searchIndicators(@RequestBody RiskIndicatorQuery query) {
        log.info("Searching risk indicators with query: {}", query);
        List<RiskIndicatorDTO> indicators = riskIndicatorService.findByQuery(query);
        return new ResponseEntity<>(indicators, HttpStatus.OK);
    }

    /**
     * 计算用户的风控指标
     */
    @PostMapping("/calculate/{userId}/{indicatorType}")
    public ResponseEntity<RiskIndicatorDTO> calculateIndicator(
            @PathVariable String userId,
            @PathVariable String indicatorType) {
        log.info("Calculating risk indicator for user: {}, type: {}", userId, indicatorType);
        RiskIndicatorDTO indicator = riskIndicatorService.calculateIndicator(userId, indicatorType);
        return new ResponseEntity<>(indicator, HttpStatus.OK);
    }

    /**
     * 批量计算用户的风控指标
     */
    @PostMapping("/calculate/batch/{indicatorType}")
    public ResponseEntity<List<RiskIndicatorDTO>> calculateIndicators(
            @RequestBody List<String> userIds,
            @PathVariable String indicatorType) {
        log.info("Calculating risk indicators for {} users, type: {}", userIds.size(), indicatorType);
        List<RiskIndicatorDTO> indicators = riskIndicatorService.calculateIndicators(userIds, indicatorType);
        return new ResponseEntity<>(indicators, HttpStatus.OK);
    }

    /**
     * 更新风控指标
     */
    @PutMapping("/{id}")
    public ResponseEntity<RiskIndicatorDTO> updateIndicator(
            @PathVariable String id,
            @RequestBody RiskIndicatorDTO indicatorDTO) {
        log.info("Updating risk indicator with ID: {}", id);
        indicatorDTO.setId(id); // 确保ID一致
        RiskIndicatorDTO updatedIndicator = riskIndicatorService.saveIndicator(indicatorDTO);
        return new ResponseEntity<>(updatedIndicator, HttpStatus.OK);
    }

    /**
     * 删除风控指标
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteIndicator(@PathVariable String id) {
        log.info("Deleting risk indicator with ID: {}", id);
        riskIndicatorService.deleteIndicator(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除用户的所有风控指标
     */
    @DeleteMapping("/user/{userId}")
    public ResponseEntity<Void> deleteUserIndicators(@PathVariable String userId) {
        log.info("Deleting all risk indicators for user: {}", userId);
        riskIndicatorService.deleteUserIndicators(userId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 根据查询条件删除风控指标
     */
    @PostMapping("/delete-by-query")
    public ResponseEntity<Long> deleteByQuery(@RequestBody RiskIndicatorQuery query) {
        log.info("Deleting risk indicators by query: {}", query);
        long deletedCount = riskIndicatorService.deleteByQuery(query);
        return new ResponseEntity<>(deletedCount, HttpStatus.OK);
    }
}